package com.flutter_ecg.flutter_ecg.ecgapplication.ecgrealtimecheck;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
//import android.support.annotation.Nullable;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ikinloop.iklibecgview.view.IKEcgView;
import com.ikinloop.iklibecgview.view.ProgressView;
import com.flutter_ecg.flutter_ecg.R;
import com.flutter_ecg.flutter_ecg.ecgapplication.utils.ShowAnimatorUtil;
import com.flutter_ecg.flutter_ecg.ecgapplication.utils.VoicePlay;
import com.flutter_ecg.flutter_ecg.ecgapplication.utils.VoicePlayMp3;

/*
    该布局类调用IKEcgCheck 接口实现了心电图的的实时绘制功能。
    外部通过该布局类的drawEcgData函数传入心电数据流，该布局类实时绘制心电图。
 */
public class IKEcgCheckLayout extends LinearLayout {

    private final String TAG = "IKEcgCheckPage:::::";

    private LinearLayout rootLayout;
    private TextView tv_heartRate;
    private IKEcgView ecgChartView;
    private ImageView iv_back;
    private ProgressView pv_timer;
    private TextView tv_check_state;

    private VoicePlayMp3 voicePlayMp3;
    private VoicePlay rawVoicePlay;

    public IKEcgCheckLayout(Context context) {
        super(context);
    }

    public IKEcgCheckLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public IKEcgCheckLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initEcgCheckLayout(Context context) {
        LayoutInflater.from(context).inflate(R.layout.ecg_check_layout, this, true);
        rootLayout = (LinearLayout) findViewById(R.id.root_layout);
        iv_back = (ImageView) findViewById(R.id.back);
        pv_timer = (ProgressView) findViewById(R.id.timer);
        pv_timer.setRightText("40s");
        tv_heartRate = (TextView) findViewById(R.id.tvBmpRate);
        tv_check_state = (TextView) findViewById(R.id.tv_check_state);
        ecgChartView = (IKEcgView) findViewById(R.id.ecgChartView);
        ecgChartView.IKEcgViewInit();
    }

    public void setRootLayoutBackGroud(Drawable backGroud) {
        rootLayout.setBackground(backGroud);
    }
    public void setRootLayoutBackGroud(int color) {
        rootLayout.setBackgroundColor(color);
    }

    public void setBackVisible(int visible) {
        iv_back.setVisibility(visible);
    }
    public void setBackClickListener(OnClickListener listener) {
        iv_back.setOnClickListener(listener);
    }

    public void setMaxTime(int duration) {
        pv_timer.setRightText(duration + "s");
        pv_timer.setProgress_max(duration);
        pv_timer.setProgress(0);
    }
    public void setDetectTime(int time) {
        pv_timer.setProgress(time);
    }

    public void setHeartRate(String heartRate) {
        tv_heartRate.setText(heartRate);
    }
    public void setHeartRateTextSize(float heartRateTextSize) {
        tv_heartRate.setTextSize(heartRateTextSize);
    }
    public void setHeartRateTextColor(int heartRateTextColor) {
        tv_heartRate.setTextColor(heartRateTextColor);
    }

    public void drawEcgData(int ecgData) {
        ecgChartView.IKEcgDrawData(ecgData);
    }
    public void IKEcgViewReset() {
        ecgChartView.IKEcgViewReset();
        pv_timer.setProgress(0);
    }

    public void setStateTextColor(int color) {
        tv_check_state.setText(color);
    }
    public void setStateTextSize(float textSize) {
        tv_check_state.setTextSize(textSize);
    }
    public void hintPressPowerBtn() {
        ShowAnimatorUtil.showTextAnimationSet(tv_check_state, getContext().getString(R.string.string_press_power_btn));
        if (voicePlayMp3 == null) {
            voicePlayMp3 = new VoicePlayMp3(getContext());
        }
        voicePlayMp3.playVoice(R.raw.ikinloop_press_power_btn);
    }
    public void hintTouchMetal() {
        ShowAnimatorUtil.showTextAnimationSet(tv_check_state, getContext().getString(R.string.string_touch_metal));
        if (voicePlayMp3 == null) {
            voicePlayMp3 = new VoicePlayMp3(getContext());
        }
        voicePlayMp3.playVoice(R.raw.ikinloop_touch_metal);
    }
    public void hintOpenBle() {
        ShowAnimatorUtil.showTextAnimationSet(tv_check_state, getContext().getString(R.string.string_bluetooth_closed));
        if (voicePlayMp3 == null) {
            voicePlayMp3 = new VoicePlayMp3(getContext());
        }
        voicePlayMp3.playVoice(R.raw.ikinloop_open_ble);
    }
    public void hintTooBigNoise() {
        ShowAnimatorUtil.showTextAnimationSet(tv_check_state, getContext().getString(R.string.string_too_big_noise));
        if (voicePlayMp3 == null) {
            voicePlayMp3 = new VoicePlayMp3(getContext());
        }
        voicePlayMp3.playVoice(R.raw.ikinloop_noise_body);
    }
    public void hintDetecting() {
        ShowAnimatorUtil.showTextAnimationSet(tv_check_state, getContext().getString(R.string.string_too_big_noise));
    }
    public void hintNoiseBigTestAgain() {
        ShowAnimatorUtil.showTextAnimationSet(tv_check_state, getContext().getString(R.string.string_nosie_big_test_agian));
        if (voicePlayMp3 == null) {
            voicePlayMp3 = new VoicePlayMp3(getContext());
        }
        voicePlayMp3.playVoice(R.raw.ikinloop_noise_big_test_again);
    }
    public void hintFindNotBoundDevice() {
        if (voicePlayMp3 == null) {
            voicePlayMp3 = new VoicePlayMp3(getContext());
        }
        voicePlayMp3.playVoice(R.raw.ikinloop_found_new_device);
    }

    public void hintLowPower() {
        ShowAnimatorUtil.showTextAnimationSet(tv_check_state, getContext().getResources().getString(R.string.string_lowpower));
    }

    public void playCheckStart() {
        if (rawVoicePlay == null) {
            rawVoicePlay = new VoicePlay();
        }
        rawVoicePlay.stop();
        rawVoicePlay.playVoice(getContext().getResources().openRawResource(R.raw.ikinloop_test_begin));
    }
    public void playCheckEnd() {
        if (rawVoicePlay == null) {
            rawVoicePlay = new VoicePlay();
        }
        rawVoicePlay.stop();
        rawVoicePlay.playVoice(getContext().getResources().openRawResource(R.raw.ikinloop_test_over));
    }

    public void stopVoice() {
        if (voicePlayMp3 != null) {
            voicePlayMp3.onPause();
        }
        if (rawVoicePlay != null) {
            rawVoicePlay.stop();
        }
    }
}
